package com.lcn.demo.service.impl;

import com.codingapi.tx.annotation.TxTransaction;
import com.lcn.demo.client.Demo2Client;
import com.lcn.demo.dao.TestRepository;
import com.lcn.demo.entity.Test;
import com.lcn.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2018/11/28 17:46
 */
@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private Demo2Client demo2Client;
    @Autowired
    private TestRepository testRepository;

    /**
     * 查询所有数据
     *
     * @return
     */
    @Override
    public List<Test> list() {
        return testRepository.findAll();
    }

    /**
     * 保存数据操作
     *
     * @return
     */
    @Override
    @TxTransaction(isStart = true)
    @Transactional
    public int save() {
        int rs2 = demo2Client.save();
        Test test = new Test();
        test.setName("jpa-hello-1");
        int rs1 = testRepository.save(test).getId();
        // 测试抛出异常，看事务回不回滚
        int v = 100 / 0;
        return rs1 + rs2;
    }
}
